home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Barras de herramientas y barras de estado / DropDownMenuButton / DropDownMenuButton.cs next >
Encoding:
Text File  |  2002-06-27  |  4.4 KB  |  129 lines

  1. //-------------------------------------------------
  2. // DropDownMenuButton.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class DropDownMenuButton: ToggleButtons
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new DropDownMenuButton());
  13.      }
  14.      public DropDownMenuButton()
  15.      {
  16.           Text = "Bot≤n de men· desplegable";
  17.           strText = "Desplegable";
  18.  
  19.                // Crea un mapa de bits para el nuevo bot≤n y lo a±ade a la lista de imßgenes.
  20.  
  21.           tbar.ImageList.Images.Add(CreateBitmapButton(clrText));
  22.  
  23.                // Crea el men· para el bot≤n.
  24.  
  25.           ContextMenu menu = new ContextMenu();
  26.  
  27.           EventHandler ehOnClick = new EventHandler(MenuColorOnClick);
  28.           MeasureItemEventHandler ehOnMeasureItem = 
  29.                     new MeasureItemEventHandler(MenuColorOnMeasureItem);
  30.           DrawItemEventHandler ehOnDrawItem =
  31.                     new DrawItemEventHandler(MenuColorOnDrawItem);
  32.  
  33.           Color[] acolor = 
  34.           { 
  35.           Color.FromArgb(0x00, 0x00, 0x00), Color.FromArgb(0x00, 0x00, 0x80),
  36.           Color.FromArgb(0x00, 0x80, 0x00), Color.FromArgb(0x00, 0x80, 0x80),
  37.           Color.FromArgb(0x80, 0x00, 0x00), Color.FromArgb(0x80, 0x00, 0x80),
  38.           Color.FromArgb(0x80, 0x80, 0x00), Color.FromArgb(0x80, 0x80, 0x80),
  39.           Color.FromArgb(0xC0, 0xC0, 0xC0), Color.FromArgb(0x00, 0x00, 0xFF),
  40.           Color.FromArgb(0x00, 0xFF, 0x00), Color.FromArgb(0x00, 0xFF, 0xFF),
  41.           Color.FromArgb(0xFF, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0xFF),
  42.           Color.FromArgb(0xFF, 0xFF, 0x00), Color.FromArgb(0xFF, 0xFF, 0xFF)
  43.           };
  44.  
  45.           for (int i = 0; i < acolor.Length; i++)
  46.           {
  47.                MenuItemColor mic = new MenuItemColor();
  48.                mic.OwnerDraw     = true;
  49.                mic.Color         = acolor[i];
  50.                mic.Click        += ehOnClick;
  51.                mic.MeasureItem  += ehOnMeasureItem;
  52.                mic.DrawItem     += ehOnDrawItem;
  53.                mic.Break         = i % 4 == 0;
  54.  
  55.                menu.MenuItems.Add(mic);
  56.           }
  57.                 // Finalmente, se construye el bot≤n.
  58.  
  59.           ToolBarButton tbarbtn = new ToolBarButton();
  60.           tbarbtn.ImageIndex = 4;
  61.           tbarbtn.Style = ToolBarButtonStyle.DropDownButton;
  62.           tbarbtn.DropDownMenu = menu;
  63.           tbarbtn.ToolTipText = "Color";
  64.  
  65.           tbar.Buttons.Add(tbarbtn);
  66.      }
  67.      void MenuColorOnClick(object obj, EventArgs ea)
  68.      {
  69.                // Establece el nuevo color del texto.
  70.  
  71.           MenuItemColor mic = (MenuItemColor) obj;
  72.           clrText = mic.Color;
  73.           panel.Invalidate();
  74.  
  75.                // Crea un nuevo mapa de bits del bot≤n.
  76.  
  77.           tbar.ImageList.Images[4] = CreateBitmapButton(clrText);
  78.           tbar.Invalidate();
  79.      }
  80.      void MenuColorOnMeasureItem(object obj, MeasureItemEventArgs miea)
  81.      {
  82.           miea.ItemHeight = 18;
  83.           miea.ItemWidth = 18;
  84.      }
  85.      void MenuColorOnDrawItem(object obj, DrawItemEventArgs diea)
  86.      {
  87.           MenuItemColor mic = (MenuItemColor) obj;
  88.           Brush brush = new SolidBrush(mic.Color);
  89.  
  90.           Rectangle rect = diea.Bounds;
  91.  
  92.           rect.X += 1;
  93.           rect.Y += 1;
  94.           rect.Width -= 2;
  95.           rect.Height -= 2;
  96.  
  97.           diea.Graphics.FillRectangle(brush, rect);
  98.      }
  99.      Bitmap CreateBitmapButton(Color clr)
  100.      {
  101.           Bitmap   bm     = new Bitmap(16, 16);
  102.           Graphics grfx   = Graphics.FromImage(bm);
  103.           Font     font   = new Font("Arial", 10, FontStyle.Bold);
  104.           SizeF    sizef  = grfx.MeasureString("A", font);
  105.           float    fScale = Math.Min(bm.Width / sizef.Width, 
  106.                                      bm.Height / sizef.Height);
  107.  
  108.           font = new Font(font.Name, fScale * font.SizeInPoints, font.Style);
  109.           StringFormat strfmt = new StringFormat();
  110.           strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
  111.  
  112.           grfx.Clear(Color.White);
  113.           grfx.DrawString("A", font, new SolidBrush(clr), 
  114.                           bm.Width / 2, bm.Height / 2, strfmt);
  115.           grfx.Dispose();
  116.  
  117.           return bm;
  118.      }
  119. }
  120. class MenuItemColor: MenuItem
  121. {
  122.      Color clr;
  123.  
  124.      public Color Color 
  125.      {
  126.           get { return clr; }
  127.           set { clr = value; }
  128.      }
  129. }